<label class="checkbox">
	<strong class="text-error"><?php echo t('page', 'Select All cPanel Accounts'); ?></strong>
	<?php echo CHtml::checkBox('checkall_cpanelaccounts', false, array('class' => 'checkallbox', 'data-checkall-select' => 'id_cpanel_accounts')); ?>
</label>
<?php echo CHtml::dropDownList('id_cpanel_accounts', '', array(), array('class' => 'chzn-select checkallselect', 'data-checkall-box' => 'checkall_cpanelaccounts', 'data-placeholder' => t('info', 'Select cPanel Accounts To Backup...'), 'multiple' => true, 'style' => 'width: 350px; tabindex=4')); ?><br />

<?php $form = $this->beginWidget('CActiveForm', array(
	'id' => 'backup-form',
	'action' => url('backup/default/do'),
	'enableClientValidation' => true,
	'clientOptions' => array(
		'validateOnSubmit' => true,
	),
)); $form = new CActiveForm(); ?>	
	<?php echo $form->dropDownList($backupForm, 'id_backup_locations', $backupForm->listBackupLocation(), array('placeholder' => $backupForm->getFieldLabel('id_backup_locations'))); ?><br />
	<?php echo $form->textField($backupForm, 'email', array('placeholder' => $backupForm->getFieldLabel('email'))); ?><br />
	<?php echo $form->textField($backupForm, 'loop_interval', array('placeholder' => $backupForm->getFieldLabel('loop_interval'))); ?><br />
	<button class="btn btn-small btn-primary" id="do_backup_btn" type="button"> <?php echo t('buttons', 'Backup Now'); ?></button><br /><br />
	
	<div class="span12 hide" id="backup_info">
		<div class="alert alter-block alert-info">
<!--			<button type="button" class="close" data-dismiss="alert">&times;</button>-->
			<div class="content"></div>
		</div>
	</div>
<?php $this->endWidget(); ?>

<script>
	$(document).ready(function(){
		$(".chzn-select").chosen(); 

		$('.checkallbox').click(function(){
			if($(this).is(':checked')){
				$('#'+$(this).attr('data-checkall-select')+' option').attr('selected', true);
			}else{
				$('#'+$(this).attr('data-checkall-select')+' option').removeAttr('selected');
			}
			$('#'+$(this).attr('data-checkall-select')).change().trigger('liszt:updated');
		});
		
		$('.checkallselect').change(function(){
			var checkallbox = $('#'+$(this).attr('data-checkall-box'));
			checkallbox.attr('checked', true);
			$.each($(this).children('option'), function(k, v){
				if(!$(v).is(':selected')){
					checkallbox.removeAttr('checked');
					return true;
				}
			});
		});

		var backupRequest = function(){
			this.requestUrl = '';
			this.id_cpanel_accounts = null;
			this.backupDetails = null;
			this.loop_interval = 0;

			this.beforeRequest = function(id_cpanel_account, doTimes){};
			this.requestCallback = function(id_cpanel_account, serverReturn, doTimes){};
			this.afterDone = function(){};

			this.doTimes = 0;
			this.do = function(){
				this.doTimes++;
				var id_cpanel_account = this.id_cpanel_accounts.pop();
				if(!id_cpanel_account){
					this.afterDone();
					return false;
				}
				this.beforeRequest(id_cpanel_account, this.doTimes);
				var thisObj = this;
				$.ajax({
					type: 'post',
					url: thisObj.requestUrl,
//					data: $.extend( 
//						{id_cpanel_account: id_cpanel_account},
//						thisObj.backupDetails
//					),
					data: 'id_cpanel_account='+id_cpanel_account+'&'+thisObj.backupDetails,
					success: function(ret){
						thisObj.requestCallback(id_cpanel_account, ret, thisObj.doTimes);
						setTimeout(function(){thisObj.do()}, thisObj.loop_interval*1000)
						return true;
					},
					dataType: 'json'
				});
			};
		};

		$('#do_backup_btn').click(function(){
			var backupDetails = $('#backup-form').serialize();
			var id_cpanel_accounts = [];
			$('#id_cpanel_accounts option').each(function(k, v){
				if($(v).is(':selected')){
					id_cpanel_accounts.push($(v).val());
				}
			});

			$('#backup_info').blink({
				'beforeCall': function(){
					$('#backup_info .content').append(
						'<p><b>Status ('+id_cpanel_accounts.length+' Account)</b></p>'
					);
				},
				'callBack': function(){
					var backupRequester = new backupRequest();
					var length = id_cpanel_accounts.length;
					backupRequester.backupDetails = backupDetails;
					backupRequester.loop_interval = $('#BackupForm_loop_interval').val();
					backupRequester.id_cpanel_accounts = id_cpanel_accounts;

					backupRequester.requestUrl = <?php echo json_encode(url('backup/default/do')); ?>;
					backupRequester.beforeRequest = function(id_cpanel_account, doTimes){
						//$('#do_backup_btn').button('loading');
						$('#backup_info .content').append(
							'<p>'+$('#id_cpanel_accounts option[value="'+id_cpanel_account+'"]').text()+': Waiting...'+' ('+doTimes+' of '+length+' Accounts)</p>'
						);
					};
					backupRequester.afterDone = function(){
						$('#do_backup_btn').button('reset');
					}
					backupRequester.requestCallback = function(id_cpanel_account, serverReturn, doTimes){
						if(serverReturn.result){
							var username = $('#id_cpanel_accounts option[value="'+id_cpanel_account+'"]').text();
							if(serverReturn.backupResult.result == 1){
								var result = '<p class="text-success accordion-toggle" data-toggle="collapse" data-target="#'+id_cpanel_account+serverReturn.time+'">&nbsp;&nbsp;&nbsp;&nbsp; <b>'+username+' - Success</b> Show/Hide Raw Output</p>';
							}else{
								var result = '<p class="text-error accordion-toggle" data-toggle="collapse" data-target="#'+id_cpanel_account+serverReturn.time+'">&nbsp;&nbsp;&nbsp;&nbsp; <b>'+username+' - Failed</b> Show/Hide Raw Output</p>';
							}
							$('#backup_info .content').append(result);
							$('#backup_info .content').append('<p id="'+id_cpanel_account+serverReturn.time+'" class="muted collapse out">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>'+serverReturn.backupResult.result+'</b></p>');
//							if(!$.isEmptyObject(serverReturn.backupResult.warnings)){
//								$('#'+id_cpanel_account+serverReturn.time).append('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Warnings:<br />');
//								$.each(serverReturn.backupResult.warnings, function(k, error){
//									$('#'+id_cpanel_account+serverReturn.time).append('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+error+'<br />');
//								});
//							}
							$('#backup_info .content').append('<p>Pause for '+backupRequester.loop_interval+' secs...</p>');
						}else{
							$.each(serverReturn.errors, function(attrName, error){
								$(error).each(function(k, e){
									$('#backup_info .content').append(
										'<p class="text-error">&nbsp;&nbsp;&nbsp;&nbsp; <b>'+$('#id_cpanel_accounts option[value="'+id_cpanel_account+'"]').text()+' - '+e+' ('+doTimes+' of '+length+' Accounts)</b></p>'
									);
								});
							});
						}
					};
					backupRequester.do();
				},
				'showTime': 0
			});
			
		});
				
	});

</script>